﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using yafc.Activity;
using yafc.Interface;

namespace yafc.UI
{
	public partial class LoginBox : Form
	{
		private readonly string TITLE_PREFIX = "登录";

		static private LoginBox instance = null;

		private string serviceName;
		private Action<string, string> okAction;
		private Action<string, string> cancelAction;		// TODO: Cancel when log error.

		public delegate bool ValidatorFunc(string Serv, string Uname, out string Reason);
		private ValidatorFunc validator;

		public string Username
		{
			get { return unameInput.Text; }
			set { unameInput.Text = value; }
		}
		public string Password
		{
			get { return passwdInput.Text; }
			set { passwdInput.Text = value; }
		}
		public string Prompt
		{
			set 
			{
				promptLabel.Text = value;
				promptLabel.Visible = true;
				unameInput.SelectAll();
				unameInput.Focus();
			}
		}
		protected Action<string, string> OkAction
		{
			get { return okAction; }
			set { okAction = value; }
		}

		protected LoginBox()
		{
			InitializeComponent();
		}

		static public LoginBox Instance()
		{
			if (instance == null)
			{
				instance = new LoginBox();
			}
			return instance;
		}

		public void Reset()
		{
			unameInput.Text = null;
			unameInput.Enabled = true;
			passwdInput.Text = null;
			passwdInput.Enabled = true;

			promptLabel.Visible = false;
			serviceName = null;

			okAction = null;
			validator = null;
		}

		public void ShowDialog(string ServiceName, Action<string, string> OkCallback, 
			ValidatorFunc Validator)
		{
			ShowDialog(ServiceName, OkCallback, null, Validator);
		}
		public void ShowDialog(string ServiceName, Action<string, string> OkCallback, 
			Action<string, string> Cancel, ValidatorFunc Validator)
		{
			serviceName = ServiceName;
			Text = TITLE_PREFIX + ServiceName;
			okAction = OkCallback;
			cancelAction = Cancel;
			validator = Validator;
			unameInput.Focus();
			ShowDialog();
		}

		public new void Hide()
		{
			Reset();
			base.Hide();
		}

		public void LoginFailed(string Prompt)
		{
			unameInput.Enabled = true;
			passwdInput.Enabled = true;
			unameInput.Focus();

			this.Prompt = Prompt;
		}

		private void ok_Click(object sender, EventArgs e)
		{
			promptLabel.Visible = true;
			if (Username.Length == 0)
			{
				promptLabel.Text = "用户名不能为空，请重新输入。";
				unameInput.Focus();
			}
			else if (Password.Length == 0)
			{
				promptLabel.Text = "密码不能为空，请重新输入。";
				passwdInput.Focus();
			}
			else
			{
				string reason;
				if (validator(serviceName, Username, out reason))
				{
					if (OkAction != null)
					{
						promptLabel.Text = "验证中，请稍候...";
						OkAction(Username, Password);
						unameInput.Enabled = false;
						passwdInput.Enabled = false;
					}
					else
					{
						throw new Exception("Don't know what to do!");
					}
				}
				else
				{
					promptLabel.Text = reason;
					unameInput.Focus();
					unameInput.SelectAll();
				}
			}
		}
		private void cancel_Click(object sender, EventArgs e)
		{
			if (cancelAction != null)
			{
				cancelAction(Username, Password);
			}
			Reset();
			Hide();
		}
		private void username_Enter(object sender, EventArgs e)
		{
			if (unameInput.Text.Length > 0)
			{
				unameInput.SelectAll();
			}
		}
		private void password_Enter(object sender, EventArgs e)
		{
			if (passwdInput.Text.Length > 0)
			{
				passwdInput.SelectAll();
			}
		}
	}
}
